layui.use([], function () {
    var element = layui.element;

    $(function () {
        element.tabChange("filter", "data", true);
    });
})

//开始采集
var startCollectState = false;
$("#start").on('click', function () {
    if (!checkDataBaseState()) {
        return;
    }
    if (startCollectState == true) {
        var index = layer.confirm('您确定要停止采集吗？', { icon: 3 }, function () {
            window.formium.postMessage("Collect", "stop");
            layer.close(index);
        });
    }
    else {
        var index = layer.confirm('点击确定开始采集', { icon: 3 }, function () {
            window.formium.postMessage("Collect", "start");
            layer.close(index);
        });
    }
})

//开始采集
function startCollect() {
    $("#report").removeClass('layui-show')
    $("#report").addClass('layui-hide')
    $("#startBtn").text("停止采集");
    startCollectState = true;
    updateChart();
}

//停止采集
function stopCollect() {
    $("#startBtn").text("开始采集");
    startCollectState = false;
    clearInterval(chartInterval);
    $("#report").removeClass('layui-hide')
    $("#report").addClass('layui-show')
}

//测压报告
$("#report").on('click', function () {
    getChartImageData()
    window.formium.postMessage("Report", chartImageData);
})

//检查数据库状态
function checkDataBaseState() {
    if (dataBaseState == false) {
        layerMessage("数据库连接失败，无法操作", 2);
    }
    return dataBaseState;
}

//检查开始采集状态
function checkStartState() {
    if (startCollectState == true) {
        layerMessage("已开始采集，无法操作", 2);
    }
    return startCollectState;
}

//数据库状态
var dataBaseState = false
//更新数据库状态
function setDataBaseState(state) {
    if (state == 1) {
        dataBaseState = true;
    }
}

//添加工程
$("#project").on('click', function () {
    window.formium.postMessage("ShowForm", "Project");
})

//添加模板
$("#template").on('click', function () {
    window.formium.postMessage("ShowForm", "Template");
})

//系统设置
$("#set").on('click', function () {
    window.formium.postMessage("ShowForm", "Set");
})

///更新项目信息
function updateProjectInfo(name) {
    $("#title").text(name);
}

var mainPressure = 0;
var remotePressure = 0;
///更新近端压力
function updateMainPressure(value, alarm) {
    mainPressure = value;
    updatePressure("#lpressure", value, alarm)
}

///更新近端压力
function updateRemotePressure(value, alarm) {
    remotePressure = value;
    updatePressure("#rpressure", value, alarm)
}

///更新压力
function updatePressure(id, value, alarm) {
    $(id).text(value);
    if (alarm == 1) {
        $(id).addClass('layui-font-red')
    }
    else {
        $(id).removeClass('layui-font-red')
    }
}

///更新近端信息
function updateMainInfo(info) {
    const object = JSON.parse(info);
    mediumTemplature = object.mediumTemplature;
    enviromentTemplature = object.enviromentTemplature;
    mainMachineTemplature = object.machineTemplature;
    $("#ltemplature").text(mediumTemplature);
    $("#etemplature").text(enviromentTemplature);
    $("#lmachinetemplature").text(mainMachineTemplature);
}

///更新远端信息
function updateRemoteInfo(info) {
    const object = JSON.parse(info);
    remoteMachineTemplature = object.machineTemplature;
    $("#rmachinetemplature").text(remoteMachineTemplature);
    $("#rtemplature").text(object.mediumTemplature);

}

//更新阶段
function updateStage(id, msg) {
    if (msg == '') {
        hiddenStage(id)
        $(id).addClass('stage-no')
    }
    else if (msg == '合格') {
        showStage(id)
        $(id).addClass('stage-no')
    } else if (msg == '不合格') {
        showStage(id)
        $(id).addClass('stage-no')
    } else if (msg == '****') {
        $(id).removeClass('stage-no')
        $(id).addClass('stage-no')
    }
    $(id).text(msg);
}

//更新阶段
function updateCurrentStage(time, name) {
    $("#stage_time").text(time);
    $("#stage_name").text(name);
}

//阶段隐藏
function hiddenStage(id) {
    $(id + "Row").removeClass('layui-show')
    $(id + "Row").addClass('layui-hide')
}

//阶段显示
function showStage(id) {
    $(id + "Row").removeClass('layui-hide')
    $(id + "Row").addClass('layui-show')
}

///更新累计时间
function updateTotalTime(time) {
    $("#total_time").text(time);
}

var chartImageData;
function getChartImageData() {
    chartImageData = pressureChart.getDataURL({
        type: "png",
        pixelRatio: 2,
        backgroundColor: "#c2c2c2",
      });
}

//当前时间
nowTime();
setInterval(() => {
    nowTime();
}, 1000);
//当前时间
function nowTime() {
    $("#time").text(moment().format("HH:mm:ss"));
    $("#date").text(moment().format("YYYY-MM-DD"));
}

//关闭按钮
$("#minimize").on('click', function () {
    window.formium.postMessage("FormState", "minimize");
})
//关闭按钮
$("#close").on('click', function () {
    var str = "您确定要关闭采集程序吗？";
    layer.confirm(str, { icon: 3 }, function () {
        window.formium.postMessage("FormState", "close");
    });
})

//消息
function layerMessage(msg, icon) {
    var a = arguments[0] ? arguments[0] : '';
    var b = arguments[1] ? arguments[1] : 7;
    if (a.length > 0) {
        layer.msg(msg, { icon: b, shade: 0.3, time: 900 });
    }
}

//loading
var loadIndex;
function layerLoading(msg) {
    loadIndex = layer.msg(msg, { icon: 16, shade: 0.3, time: 0 });
}
//loading结束
function endLayerLoading() {
    layer.close(loadIndex);
}